import type { User } from "~/types/user";
import { ref } from "vue";

export const useUserStore = defineStore(
  "user",
  () => {
    const user = ref<User | null>(null);
    const token = ref<string | null>(null);

    const setUser = (newUser: User | null) => {
      user.value = newUser;
    };

    const setToken = (newToken: string | null) => {
      token.value = newToken;
    };

    // 清除用户信息
    const clearAuth = () => {
      user.value = null;
      token.value = null;
    };

    return {
      user,
      setUser,
      token,
      setToken,
      clearAuth,
    };
  },
  {
    persist: true,
  }
);
